package viroyal.com.base.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import viroyal.com.base.R;
import viroyal.com.base.activity.main.MainDelegate;
import viroyal.com.base.listener.OnClickEffectiveListener;
import viroyal.com.base.model.Announced;
import viroyal.com.base.util.HtmlToStringUtil;
import viroyal.com.base.widget.AutoScrollView;


/**
 * @author chenjunwei
 * @desc 通知
 * @date 2019/5/22
 */
public class ItemNoticePagerAdapter extends PagerAdapter {
  private MainDelegate mainDelegate;
  private Context context;
  private List<Announced> announcedList;
  private ViewPager viewPager;
  private String TAG = getClass().getSimpleName();
  private int currentPosition;
  private View view;


  public void adapterNotifyDataSetChanged(List<Announced> announcedList) {
    this.announcedList = announcedList;
    notifyDataSetChanged();
  }

  public ItemNoticePagerAdapter(MainDelegate mainDelegate, List<Announced> announcedList, Context context, ViewPager viewPager) {
    this.mainDelegate = mainDelegate;
    this.context = context;
    this.announcedList = announcedList;
    this.viewPager = viewPager;
  }

  @Override
  public int getCount() {
    //无限轮播
//    return null != announcedList ? announcedList.size() : 0;
    return Integer.MAX_VALUE;
  }


  @Override
  public boolean isViewFromObject(View view, Object object) {
    return view == object;
  }

  @Override
  public Object instantiateItem(ViewGroup container, int viewPosition) {
    if (android.os.Build.MODEL.equals("T2") || android.os.Build.MODEL.equals("T3") || android.os.Build.MODEL.equals("T6")) {
      //新班牌(华瑞安)
      view = LayoutInflater.from(context).inflate(R.layout.item_notice_layout_new, container, false);
    } else if (android.os.Build.MODEL.equals("rk3288")) {
      //广州星马(班牌)
      view = LayoutInflater.from(context).inflate(R.layout.item_notice_layout_new_xm, container, false);
    } else {
      view = LayoutInflater.from(context).inflate(R.layout.item_notice_layout_new, container, false);
    }
    AutoScrollView scrollView = view.findViewById(R.id.sv_notice);
    TextView tv_item_notice = (TextView) view.findViewById(R.id.tv_item_notice);
    LinearLayout ll_notice = (LinearLayout) view.findViewById(R.id.ll_notice);
    TextView tv_item_content = view.findViewById(R.id.tv_item_content);
    Log.i(TAG, "instantiateItem: viewPosition"+viewPosition);
    //无限轮播
    if (announcedList.size() > 0) {
      Announced announced = announcedList.get(viewPosition%announcedList.size());
        if (viewPosition == 0 || currentPosition == viewPosition) {
          scrollView.setPosition(currentPosition);
          scrollView.setScrolled(true);
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

          }
          @Override
          public void onPageSelected(int position) {
            Log.i(TAG, "onPageSelected: position"+position);
            currentPosition = position;
              if(viewPosition == position){
                scrollView.setPosition(position);
                scrollView.setScrolled(true);
              } else {
                scrollView.setScrolled(false);
              }
          }
          @Override
          public void onPageScrollStateChanged(int state) {

          }
        });
      tv_item_notice.setText(announced.title);
      tv_item_content.setText(HtmlToStringUtil.getHtmlContent(announced.value));
      ll_notice.setOnClickListener(new OnClickEffectiveListener() {
        @Override
        public void onClickEffective(View v) {
          if (null != mainDelegate) {
            if (TextUtils.isEmpty(announced.value)) {
              mainDelegate.showNoticeDetailsDialog(announced);
            } else {
              mainDelegate.showNoticeDetailsDialogFragment(announced);
            }
          }
        }
      });
    }
    container.addView(view);
    return view;
  }

  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
  }

  @Override
  public int getItemPosition(@NonNull Object object) {
    return POSITION_NONE;
  }
}
